function changePage(id){
	var $target = $("#operate").find("[data-id='"+id+"']");
	var $all = $("#operate").children(".animate");
	$all.css("left","-100%");
	$all.hide();
	$target.show().animate({left:""},"fast");	
}
function ajax_send(id,page,confirm,page_size){
	var area;
	var area_father;
	if(id ==1){
		area = $(".OneItem");
		area_father = $("#game table tbody");
	}else if(id == 2){
		area = $(".TwoItem");
		area_father = $("#team table tbody");
	}else if(id == 3){
		area = $(".ThreeItem");
		area_father = $("#member table tbody");
	}else if(id ==4){
		area = $(".FourItem");
		area_father = $("#verify table tbody");
	}else if(id ==5){
		area = $(".FiveItem");
		area_father = $("#choice table tbody");
	}
	$.ajax({
		type:"GET",
		url:"./changePage.php?id="+id+"&page="+page+"&page_size="+page_size,
		async:confirm,
		success: function(data){
			var result = jQuery.parseJSON(data);
			area.remove();


			for(var i in result){
				var item = result[i];
				if(id == 1){
					In_Html =" <tr class='OneItem'><th class='data-name'>"+item.name+"</th><th class='data-category'>"+item.category+"</th><th class='data-files'><a href='"+item.files+"' target='_blank'>"+item.files+"</a></th><th class='data-value'>"+item.value+"</th><th class='data-description'>"+item.description+"</th><th class='data-flag'>"+item.flag+"</th><th><button type='button' data-did='modify'>修改</button> <button type='button' data-did='delete' sql_id='"+item.id+"'>删除</button></th> </tr>";
				}else if(id == 2){
					item = result[i][0];
					In_Html =  "<tr class='TwoItem'><th class='data-name'>"+item.name+"</th><th class='data-email'>"+item.email+"</th><th class='data-number'>"+item.number+"</th>";

					In_Html += "<th class='member'>";
					for(var j in result[i]){
						In_Html += "<a class='team_member_change' data-id='"+result[i][j]['member_id']+"'>"+result[i][j]['member_name']+"</a>";
						In_Html += "&nbsp;";
					}
					In_Html += "<th class='data-phone'>"+item.phone+"</th><th class='data-school'>"+item.school+"</th><th><button type='button' data-did='modify'>修改</button> <button type='button' data-did='delete' sql_id='"+item.id+"'>删除</button></th></tr>"
				}else if(id == 3){
					item = result[i][0];
					In_Html =" <tr class='ThreeItem'>\
								<th calss='data-id'>"+item.student_id+"</th>\
								<th calss='data-name'>"+item.name+"</th>"

					In_Html+="<th calss='data-team_id'>";
					for(var j in result[i]){
						In_Html += result[i][j]['team_name'];
					}


					In_Html+="<th><button type='button' data-did='modify'>修改</button>\
								<button type='button' sql_id='"+item.id+"' data-did='delete'>删除</button>\
								</th>\
								</tr>"
				}else if(id ==4){
					item = result[i][0];
					In_Html =" <tr class='FourItem'><th class='data-name'>"+item.name+"</th>\
							   <th class='data-email'>"+item.email+"</th>\
							   <th class='data-password'>"+item.password+"</th>"
							   for(var j in result[i]){
							   		In_Html += "<th class='data-member_name'>"+result[i][j]['member_name']+"</th>";
							   		In_Html += "<th class='data-member_id'>"+result[i][j]['student_id']+"</th>";
							
								}
					In_Html +=" <th class='data-phone'>"+item.phone+"</th>\
							   <th class='data-school'>"+item.school+"</th>\
							   <th><button type='button' data-did='agree'>同意</button> <button type='button' data-did='refuse' sql_id='"+item.id+"'>拒绝</button></th></tr>";
				}else if(id ==5){
					In_Html = "\
					<tr class='FiveItem'>\
						<th class='data-content'>"+item.content+"</th>\
						<th class='data-answer'>"+item.answer+"</th>\
						<th class='data-enable'>"+item.enable+"</th>\
						<th class='data-radio'>"+jQuery.parseJSON(item.radio)+"</th>\
						<th>\
							<button type='button' data-did='modify'>修改</button>\
							<button type='button' data-did='delete' sql_id='"+item.id+"'>删除</button>\
						</th>\
					</tr>";
				}
				area_father.append(In_Html);
			}
		}
	})			
			
}
function check_back(state){
	if(state == 200){
		alert("提交成功");
	}else{
		alert("提交失败");
	}	
};
$(document).ready(function(){	
	var id = location.hash.substr(1) || 1;	
	var sql_id ;
	var add = 0;
	changePage(id);
	var page = 1;
	var page_size = 12;
	var pagetotal_challenge;
	var pagetotal_team;
	var pagetotal_member;
	var pagetotal_verify
	var pagetotals;
	var tmp_id;

	for(var i=1;i<6;i++){
		ajax_send(i,1,false,page_size);
	 }
	$.ajax({
		type:"GET",
		async:false,
		url:"./changePage.php?pagetotal=1&page_size="+page_size,
		success: function(data){

			var result = jQuery.parseJSON(data);
			
			pagetotal_challenge = result[0];
			pagetotal_team = result[1];
			pagetotal_member = result[2];
			pagetotal_verify = result[3];
			pagetotal_choice = result[4];
		}
	});
	function choose_pagetotal(id){
			if(id == 1){
				return pagetotal_challenge;
			}else if(id == 2){
				return pagetotal_team;
			}else if (id ==3){
				return pagetotal_member;
			}else if (id ==4){
				return pagetotal_verify;
			}else if (id ==5){
				return pagetotal_choice;
			}
			
		}
	var target = $("#operate").find("[data-id='"+id+"']");
	var all_page = target.children(".all_page");
	all_page.children(".surr_page").empty();
	all_page.children(".surr_page").append(page+"/"+choose_pagetotal(id));
	$("span").click(function(){
		id = $(this).attr("data-id");
		location.hash = id;
	    changePage(id);
	    page = 1;
	    ajax_send(id,page,true,page_size);
	    var target = $("#operate").find("[data-id='"+id+"']");
	    var all_page = target.children(".all_page");
	    if(id == 1){
	    	pagetotal = pagetotal_challenge;
	    }else if(id ==2){
	    	pagetotal = pagetotal_team;
	    }else if(id == 3){
	    	pagetotal = pagetotal_member;
	    }else if(id == 4){
	    	pagetotal = pagetotal_verify;
	    }else if (id == 5){
			pagetotal = pagetotal_choice;
		}
		all_page.children(".surr_page").empty();
		all_page.children(".surr_page").append(page+"/"+choose_pagetotal(id));
	});
	$("#operate").on("click", "button", function(){				//点击修改或删除
		
		if($(this).attr("data-did")=="delete"){ //点击删除
		    sql_id = $(this).attr("sql_id");
			var r = confirm("确定删除?");
			if(r == true){
				$.ajax({
					 type: 'GET',
					 url: "./operation.php?id="+id+"&opera=2&sql_id="+sql_id,
					 success: function(data){
					 	check_back(data);
					 	history.go(0);
					 }
				})
			}
		}else if($(this).attr("data-did")=="refuse"){
			sql_id = $(this).attr("sql_id");
			$.ajax({
				 type: 'GET',
				 url: "./operation.php?id="+id+"&opera=2&sql_id="+sql_id,
				 success: function(data){
				 	check_back(data);
				 	history.go(0);
				 }
			})
		}else if($(this).attr("data-did")=="agree"){
			sql_id = $(this).next().attr("sql_id");
			$.ajax({
					 type: 'GET',
					 url: "./operation.php?id="+id+"&opera=3&sql_id="+sql_id,
					 success: function(data){
					 	check_back(data);
					 	history.go(0);
					 }
			})
		}
		else if($(this).attr("data-did")=="modify"){	//点击修改


			if(id==1){
				$("#add_challenge_div").show();  //修改题目对话框
				var information = $(this).parents(".OneItem");
				var name = information.children(".data-name");
				var category = information.children(".data-category");
				var files = information.children(".data-files");
				var value = information.children(".data-value");
				var flag = information.children(".data-flag");
				var description = information.children(".data-description");
				sql_id = $(this).next().attr("sql_id");
				$(".challenge_css").eq(0).val(name.text());
				$(".challenge_css").eq(1).val(category.text());
				$(".challenge_css").eq(2).val(value.text());
				$(".challenge_css").eq(3).val(files.text());
				$(".challenge_css").eq(4).val(flag.text());
				$(".challenge_css").eq(5).val(description.text());
				$("#add_challenge_div").animate({top:""},"slow");
			} else if(id==2) {     //修改队伍对话框
				$("#add_team_div").show();
				var informations = $(this).parents(".TwoItem");
				var name = informations.children().eq(0);
				var email = informations.children().eq(1);
				var number = informations.children().eq(2);
				var phone = informations.children().eq(4);
				var school = informations.children().eq(5);
				sql_id = $(this).next().attr("sql_id");
				$(".team_css").eq(0).val(name.text());
				$(".team_css").eq(1).val(email.text());
				$(".team_css").eq(3).val(number.text());
				$(".team_css").eq(4).val(phone.text());
				$(".team_css").eq(5).val(school.text());
				$("#add_team_div").animate({top:""},"slow");
			}else if(id==3){   //修改成员对话框
				$("#add_member_div").show();
				sql_id = $(this).next().attr("sql_id");
				var information = $(this).parents(".ThreeItem");
				var studentid = information.children().eq(0).text();
				var name = information.children().eq(1).text();
				var team_name = information.children().eq(2).text();
				$(".member_css").eq(0).val(studentid);
				$(".member_css").eq(1).val(name);
				$.ajax({
					type:'get',
					url:"./operation.php?id=2&opera=4&student_id="+studentid,
					success:function(data){
						result = jQuery.parseJSON(data);
						$(".team_select").empty();
						for(var i=0;i<result.length;i++){
							var name = result[i].name;
							var team_ids = result[i].id
							if(name == team_name){
								var name = result[i].name;
								$(".team_select").append("<option value='"+team_ids+"'>"+name+"</option>");
							}
						}
						for(var i=0;i<result.length;i++){
							var name = result[i].name;
							var team_ids = result[i].id;
							if(name != team_name){
								$(".team_select").append("<option value='"+team_ids+"'>"+name+"</option>");
							}
						}
					}
				});
				$(".team_select").append()
				$("#add_member_div").animate({top:""},"slow");
			}else if(id==5){   //修改选择题对话框
				$("#add_choice_div").show();
				sql_id = $(this).next().attr("sql_id");
				var information = $(this).parents(".FiveItem");
				var content = information.children().eq(0).text();
				var answer = information.children().eq(1).text();
				var enable = information.children().eq(2).text();
				var radios;
				$.ajax({
					type:'get',
					url:"./operation.php?id="+id+"&opera=4&sql_id="+sql_id,
					async:false,
					success:function(data){

						result = jQuery.parseJSON(data);
						radios = jQuery.parseJSON(result.radio);
					}
				});
				$(".choice_css").eq(0).val(content);
				$(".choice_css").eq(1).val(answer);
				$(".choice_css").eq(2).val(radios[0]);
				$(".choice_css").eq(3).val(radios[1]);
				$(".choice_css").eq(4).val(radios[2]);
				$(".choice_css").eq(5).val(radios[3]);
				$("#add_choice_div").animate({top:""},"slow");
			}
		}
	});
	$(".close_down").click(function(){
		if(id==1){
			$("#add_challenge_div").animate({top:"-1000px"},"slow");
			$(".input_css").val("");
		}else if(id==2){
			$("#add_team_div").animate({top:"-1000px"},"slow");
			$(".input_css").val("");
			$("#add_member_div").animate({top:"-1000px"},"slow");
			$(".input_css").val("");
			$(".member_insert").hide();
		}else if(id==3){
			$("#add_member_div").animate({top:"-1000px"},"slow");
			$(".input_css").val("");
		}else if(id==5){
			$("#add_choice_div").animate({top:"-1000px"},"slow");
			$(".input_css").val("");
		}
	});
	$("#challenge_submit").click(function(){	//提交题目修改
		var name = $(".challenge_css").eq(0).val();
		var category = $(".challenge_css").eq(1).val();
		var value = $(".challenge_css").eq(2).val();
		var files = $(".challenge_css").eq(3).val();
		var flag  = $(".challenge_css").eq(4).val();
		var description = $(".challenge_css").eq(5).val();
		if(add == 1){
			$.ajax({
			 	type: 'GET',
			 	url: "./operation.php?id="+id+"&opera=1&name="+name+"&category="+category+"&value="+value+"&files="+files+"&description="+description+"&flag="+flag,
			 	success: function(data){
			 		check_back(data);
			 		history.go(0);
			 	}
			});

		}else{
			$.ajax({
				 type: 'GET',
				 url: "./operation.php?id="+id+"&opera=3&sql_id="+sql_id+"&name="+name+"&category="+category+"&value="+value+"&files="+files+"&description="+description,
				 success: function(data){
				 	check_back(data);
			 		history.go(0);
				 }
			});
		}
	});
	$("#team_submit").click(function(){   //提交队伍修改
		var name = $(".team_css").eq(0).val();
		var email = $(".team_css").eq(1).val();
		var password = $(".team_css").eq(2).val();
		var number = $(".team_css").eq(3).val();
		var phone = $(".team_css").eq(4).val();
		var school = $(".team_css").eq(5).val();
		var student_id = $(".member_inserts").eq(0).val();
		var member_name = $(".member_inserts").eq(1).val();
		if(add == 1){
			var number = 1;
			$.ajax({
			 	type: 'GET',
			 	url: "./operation.php?&id="+id+"&opera=1&&name="+name+"&email="+email+"&password="+password+"&number="+number+"&phone="+phone+"&school="+school+"&student_id="+student_id+"&member_name="+member_name,
			 	success: function(data){
			 		check_back(data);
			 		history.go(0);
			 	}
			});
		}else{
			if(password == ""){
				
				$.ajax({
				 	type: 'GET',
				 	url: "./operation.php?id="+id+"&opera=3&sql_id="+sql_id+"&name="+name+"&email="+email+"&number="+number+"&phone="+phone+"&school="+school,
				 	success: function(data){
				 		check_back(data);
				 		history.go(0);
				 	}
				});

			}else{
				
				$.ajax({
					 type: 'GET',
					 url: "./operation.php?id="+id+"&opera=3&sql_id="+sql_id+"&name="+name+"&email="+email+"&password="+password+"&number="+number+"&phone="+phone+"&school="+school,
					 success: function(data){
					 	
					 	check_back(data);
					 	history.go(0);
					 }
				});
			}
		}
	});
	$("#member_submit").click(function(){
		
		var student_id = $(".member_css").eq(0).val();
		var name = $(".member_css").eq(1).val();
		var team_id = $(".team_select").val();
		if(add ==1){
			$.ajax({
			 	type: 'GET',
			 	url: "./operation.php?id=3&opera=1&name="+name+"&student_id="+student_id+"&team_id="+team_id,
			 	success: function(data){
			 		check_back(data);
			 		history.go(0);
			 	}
			});
		}else{
	
			$.ajax({
				type: 'GET',
				url: "./operation.php?id=3&opera=3&sql_id="+sql_id+"&name="+name+"&student_id="+student_id+"&team_id="+team_id,
				success: function(data){
				check_back(data);
					history.go(0);
				}
			});
		}

	});
	$("#choice_submit").click(function(){
		var content = $(".choice_css").eq(0).val();
		var answer = $(".choice_css").eq(1).val();
		var enable = $(".yes_or_no").val();
		var radios = new Array();
		for(var i=0;i<4;i++){
			var tmp =2+i;
			radios.push($(".choice_css").eq(tmp).val())
		}
		radio =JSON.stringify(radios);
		if(add ==1){
			$.ajax({
			 	type: 'GET',
			 	url: "./operation.php?id="+id+"&opera=1&content="+content+"&answer="+answer+"&enable="+enable+"&radio="+radio,
			 	success: function(data){
			 		check_back(data);
			 		history.go(0);
			 	}
			});
		}else{
			$.ajax({
				 type: 'GET',
				 url: "./operation.php?id="+id+"&opera=3&sql_id="+sql_id+"&content="+content+"&answer="+answer+"&enable="+enable+"&radio="+radio,
				 success: function(data){
				 	check_back(data);
				 	history.go(0);
				 }
			});
		}
		
	})

	$(".add").click(function(){ //添加成员
		if (id==1){
			$("#add_challenge_div").show();
			$("#add_challenge_div").animate({top:""},"slow");
			add = 1;

		}else if(id==2){
			$("#add_team_div").show();
			$(".member_insert").show();
			$("#add_team_div").animate({top:""},"slow");
			add = 1;
		}else if(id==3){
			$("#add_member_div").show();
			$("#add_member_div").animate({top:""},"slow");
			$.ajax({
					type:'get',
					url:"./operation.php?id=2&opera=4",
					success:function(data){
						result = jQuery.parseJSON(data);
						$(".team_select").empty();
						for(var i=0;i<result.length;i++){
							var team_id = result[i].id;
							var name = result[i].name;
							$(".team_select").append("<option value='"+team_id+"'>"+name+"</option>");
						}
					}
				});
			add = 1;
		}else if(id==5){
			$("#add_choice_div").show();
			$("#add_choice_div").animate({top:""},"slow");
			add = 1;

		}
	});
	$(".next_page").mouseover(function(){
		$(this).css("cursor","pointer");
	})
	$(".next_page").click(function(){
			function choose_pagetotal(id){
				if(id == 1){
					return pagetotal_challenge;
				}else if(id == 2){
					return pagetotal_team;
				}else if (id ==3){
					return pagetotal_member;
				}else if (id ==4) {
					return pagetotal_verify;
				}else if (id ==5) {
					return pagetotal_choice;
				}
			
			}
		var new_id = $(this).parents(".animate").attr("data-id");
		page += 1;
		if (page>choose_pagetotal(new_id)) { page = choose_pagetotal(new_id);return};
		ajax_send(new_id,page,false,page_size);
		$(this).prev().empty();
		$(this).prev().append(page+"/"+choose_pagetotal(new_id));
		
	})
	$(".pre_page").mouseover(function(){
		$(this).css("cursor","pointer");
	})
	$(".pre_page").click(function(){
		function choose_pagetotal(id){
				if(id == 1){
					return pagetotal_challenge;
				}else if(id == 2){
					return pagetotal_team;
				}else if (id ==3) {
					return pagetotal_member;
				}else if (id ==4){
					return pagetotal_verify;
				}else if (id ==5) {
				return pagetotal_choice;
				}
			
			}
		var new_id = $(this).parents(".animate").attr("data-id");
		page -= 1;
		if (page<1) { page = 1;return};
		ajax_send(new_id,page,false,page_size);
		$(this).next().empty();
		$(this).next().append(page+"/"+choose_pagetotal(new_id));
	})
	$("#team").on("click",".team_member_change",function(){
		sql_id = $(this).attr("data-id");
		$("#add_member_div").show();
		var studentid ;
		var name ;
		var team_id ;
		$.ajax({
			type:'get',
			url:"./operation.php?id=2&opera=5&member_id="+sql_id,
			async:false,
			success:function(data){
				var result = jQuery.parseJSON(data);
				studentid = result.student_id;
				name = result.name;
				team_id = result.team_id;
			}
		})
		$(".member_css").eq(0).val(studentid);
		$(".member_css").eq(1).val(name);
		$.ajax({
			type:'get',
			url:"./operation.php?id=2&opera=4&student_id="+studentid,
			success:function(data){
				result = jQuery.parseJSON(data);
				$(".team_select").empty();
				for(var i=0;i<result.length;i++){
					var id = result[i].id;
					if(id == team_id){
						var name = result[i].name;
						$(".team_select").append("<option value='"+id+"'>"+name+"</option>");
					}
				}
				for(var i=0;i<result.length;i++){
					var id = result[i].id;
					if(id != team_id){
						var name = result[i].name;
						$(".team_select").append("<option value='"+id+"'>"+name+"</option>");
					}
				}
			}
		});
		$(".team_select").append();
		$("#add_member_div").animate({top:""},"slow");
		add=2;
		tmp_id =sql_id;
	})
})